package com.softwinner.awbt.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.RemoteException;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.Toast;

import com.softwinner.awbt.R;
import com.softwinner.awbt.adapter.MainAdapter;
import com.softwinner.awbt.controller.BTController;
import com.softwinner.awbt.fragment.CallLogFragment;
import com.softwinner.awbt.fragment.ContactsFragment;
import com.softwinner.awbt.fragment.DialFragment;
import com.softwinner.awbt.fragment.MusicFragment;
import com.softwinner.awbt.fragment.SettingsFragment;
import com.softwinner.awbt.listener.CallListener;
import com.softwinner.awbt.utils.LOG;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements CallListener {

    private ViewPager mViewPager;
    private BottomNavigationView mBottomNavigationView;
    private List<Fragment> mFragments = new ArrayList<>();
    private static CallListener sCallListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LOG.d("--MainActivity    onCreate");
        setContentView(R.layout.activity_main);
        initView();
        sCallListener = this;
        BTController.getInstance().init(getApplicationContext());
    }

    @Override
    protected void onStart() {
        LOG.d("---MainActivity onStart");
        super.onStart();
    }

    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        mBottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);
        mFragments.add(DialFragment.newInstance());
        mFragments.add(ContactsFragment.newInstance());
        mFragments.add(CallLogFragment.newInstance());
        mFragments.add(MusicFragment.newInstance());
        mFragments.add(SettingsFragment.newInstance());
        mViewPager.setAdapter(new MainAdapter(getSupportFragmentManager(), mFragments));
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int
                    positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                int id = R.id.navigation_dial;
                switch (position) {
                    case 0:
                        id = R.id.navigation_dial;
                        break;
                    case 1:
                        id = R.id.navigation_contact;
                        break;
                    case 2:
                        id = R.id.navigation_history;
                        break;
                    case 3:
                        id = R.id.navigation_music;
                        break;
                    case 4:
                        id = R.id.navigation_settings;
                        break;
                }
                if (mBottomNavigationView.getSelectedItemId() != id) {
                    mBottomNavigationView.setSelectedItemId(id);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mViewPager.setCurrentItem(4);
        mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView
                .OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.navigation_dial:
                        if (mViewPager.getCurrentItem() != 0) {
                            mViewPager.setCurrentItem(0);
                        }
                        return true;
                    case R.id.navigation_contact:
                        if (mViewPager.getCurrentItem() != 1) {
                            mViewPager.setCurrentItem(1);
                        }
                        return true;
                    case R.id.navigation_history:
                        if (mViewPager.getCurrentItem() != 2) {
                            mViewPager.setCurrentItem(2);
                        }
                        return true;
                    case R.id.navigation_music:
                        if (mViewPager.getCurrentItem() != 3) {
                            mViewPager.setCurrentItem(3);
                        }
                        return true;
                    case R.id.navigation_settings:
                        if (mViewPager.getCurrentItem() != 4) {
                            mViewPager.setCurrentItem(4);
                        }
                        return true;
                }
                return false;
            }
        });
    }

    @Override
    protected void onDestroy() {
        LOG.d("--MainActivity    onStop");
        sCallListener = null;
        super.onDestroy();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public void onBackPressed() {

    }


    public void doScan() {
        try {
            BTController.getInstance().doScan();
        } catch (RemoteException e) {
            showMessage(R.string.remote_exception_alert);
        }
    }

    public void doRename(String name) {
        try {
            BTController.getInstance().doRename(name);
        } catch (RemoteException e) {
            showMessage(R.string.remote_exception_alert);
        }
    }

    public void doConnect(String address) {
        try {
            BTController.getInstance().doConnect(address);
        } catch (RemoteException e) {
            showMessage(R.string.remote_exception_alert);
        }
    }

    public void setAutoConnect(boolean auto) {
        try {
            BTController.getInstance().setAutoConnect(auto);
        } catch (RemoteException e) {
            showMessage(R.string.remote_exception_alert);
        }
    }

    public void setAutoAnswer(boolean auto) {
        try {
            BTController.getInstance().setAutoAnswer(auto);
        } catch (RemoteException e) {
            showMessage(R.string.remote_exception_alert);
        }
    }

    public void disConnected() {
        try {
            BTController.getInstance().disConnected();
        } catch (RemoteException e) {
            showMessage(R.string.remote_exception_alert);
        }
    }

    public void showMessage(int message) {
        if (mBottomNavigationView != null) {
            Snackbar.make(mBottomNavigationView, message, Snackbar.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        }

    }

    public void playOrPauseMusic() {
        try {
            BTController.getInstance().playOrPauseMusic();
        } catch (RemoteException e) {
            showMessage(R.string.remote_exception_alert);
        }
    }

    public void toNextMusic() {
        try {
            BTController.getInstance().toNextMusic();
        } catch (RemoteException e) {
            showMessage(R.string.remote_exception_alert);
        }
    }

    public void toPreviousMusic() {
        try {
            BTController.getInstance().toPreviousMusic();
        } catch (RemoteException e) {
            showMessage(R.string.remote_exception_alert);
        }
    }

    public void getMusicInfo() {
        try {
            BTController.getInstance().getMusicInfo();
        } catch (RemoteException e) {
            showMessage(R.string.remote_exception_alert);
        }
    }


    public void callPhone(String number) {
        try {
            BTController.getInstance().callPhone(number);
        } catch (RemoteException e) {
            showMessage(R.string.remote_exception_alert);
        }
    }


    /**
     * @param type 1: call out
     *             2: missed call
     *             3: call in
     */
    public void syncCallLogs(int type) {
        try {
            BTController.getInstance().syncCallLogs(type);
        } catch (RemoteException e) {
            showMessage(R.string.remote_exception_alert);
        }
    }

    public void syncContacts() {
        try {
            BTController.getInstance().syncContacts();
        } catch (RemoteException e) {
            showMessage(R.string.remote_exception_alert);
        }
    }

    @Override
    public void onCall(String number) {
        Intent intent = new Intent(this, CallActivity.class);
        intent.putExtra("callNumber", number);
        intent.putExtra("isConnect", false);
        startActivity(intent);
    }

    @Override
    public void onHangUp() {

    }

    @Override
    public void onTaking() {

    }

    @Override
    public void onCallIn(String number) {
        LOG.d("MainActivity incoming number is " + number);
        Intent intent = new Intent(this, InComingActivity.class);
        intent.putExtra("incomingNumber", number);
        startActivity(intent);
    }

    @Override
    public void onConnected() {
        showMessage(R.string.connected);
    }

    @Override
    public void onDisConnected() {
        showMessage(R.string.disconnect);
    }

    public static CallListener getCallListener() {
        return sCallListener;
    }
}
